package com.example.demo.codegenerate.service;

import com.example.demo.codegenerate.Config;
import com.example.demo.codegenerate.util.CodeGenerateUtil;
import com.example.demo.codegenerate.util.MyFreeMarkerTemplateUtils;
import com.example.demo.codegenerate.dao.CodeGenerateDao;
import com.example.demo.dao.Creation;
import com.example.demo.codegenerate.dao.JeecgColumnDao;
import com.example.demo.codegenerate.dao.JeecgTableDao;
import com.example.demo.codegenerate.domain.DatabaseTable;
import com.example.demo.codegenerate.domain.JeecgColumn;
import com.example.demo.codegenerate.domain.JeecgTable;
import freemarker.template.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.example.demo.dao.Creation.Operation.eq;

@Service
public class CodeGenerateService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JeecgTableDao jeecgTableDao;

    @Autowired
    private JeecgColumnDao jeecgColumnDao;

    @Autowired
    private CodeGenerateDao codeGenerateDao;

    @Autowired
    private Configuration configuration;

    @Autowired
    private Config config;

    public void generate(String jeecgTableId){
        generate(jeecgTableId, config.get_package(), config.getBasePath());
    }

    /**
     * 生成代码
     * @param jeecgTableId
     * @param _package
     */
    public void generate(String jeecgTableId, String _package, String basePath){
        JeecgTable jeecgTable = searchJeecgTable(jeecgTableId);

        String srcPath = basePath + "/java/" + _package.replace(".", "/");

        String mapperPath = basePath + "/resources/mapper";

        String viewPath = basePath + "/resources/templates/buss";

        Map map = CodeGenerateUtil.renderParameter(_package, jeecgTable);

        String className = (String)map.get("className");
        String classNameFirstLower = (String)map.get("classNameFirstLower");

        try {
            // domain
            MyFreeMarkerTemplateUtils.processTemplateIntoFile(
                    configuration.getTemplate("sys/codeGenerate/domain.ftl"), map,
                    String.format("%s/%s/%s%s.%s", srcPath, "domain", className, "", "java"));
            logger.debug("generate domain file success:{}", className);

            //dao
            MyFreeMarkerTemplateUtils.processTemplateIntoFile(
                    configuration.getTemplate("sys/codeGenerate/dao.ftl"), map,
                    String.format("%s/%s/%s%s.%s", srcPath, "dao", className, "Dao", "java"));
            logger.debug("generate dao file success:{}", className);

            //mapper
            MyFreeMarkerTemplateUtils.processTemplateIntoFile(
                    configuration.getTemplate("sys/codeGenerate/mapper.ftl"), map,
                    String.format("%s/%s%s.%s", mapperPath, className, "Mapper", "xml"));
            logger.debug("generate mapper file success:{}", className);

            //service
            MyFreeMarkerTemplateUtils.processTemplateIntoFile(
                    configuration.getTemplate("sys/codeGenerate/service.ftl"), map,
                    String.format("%s/%s/%s%s.%s", srcPath, "service", className, "Service", "java"));
            logger.debug("generate service file success:{}", className);

            //controller
            MyFreeMarkerTemplateUtils.processTemplateIntoFile(
                    configuration.getTemplate("sys/codeGenerate/controller.ftl"), map,
                    String.format("%s/%s/%s%s.%s", srcPath, "controller", className, "Controller", "java"));
            logger.debug("generate controller file success:{}", className);

            //list
            MyFreeMarkerTemplateUtils.processTemplateIntoFile(
                    configuration.getTemplate("sys/codeGenerate/list.ftl"), map,
                    String.format("%s/%s/%s.%s", viewPath, classNameFirstLower, "list",  "ftl"));
            logger.debug("generate list file success:{}", className);

            //add
            MyFreeMarkerTemplateUtils.processTemplateIntoFile(
                    configuration.getTemplate("sys/codeGenerate/add.ftl"), map,
                    String.format("%s/%s/%s.%s", viewPath, classNameFirstLower, "add",  "ftl"));
            logger.debug("generate add file success:{}", className);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JeecgTable searchJeecgTable(String jeecgTableId){
        JeecgTable jeecgTable = jeecgTableDao.get(jeecgTableId);
        List<JeecgColumn> jeecgColumns = jeecgColumnDao.search(new Creation(
                "jeecg_table_id", eq, jeecgTable.getId()));
        jeecgTable.setJeecgColumnList(jeecgColumns);
        return jeecgTable;
    }

    @Transactional
    public void syncDatabase(String jeecgTableId) {
        JeecgTable jeecgTable = searchJeecgTable(jeecgTableId);

        if(jeecgTable != null){
            DatabaseTable databaseTable = CodeGenerateUtil.jeecgTable2databaseTable(jeecgTable);
            codeGenerateDao.createTable(databaseTable);
        }
    }
}
